package com.danciyixue.boot.dcyx.controller;

import com.danciyixue.boot.dcyx.service.TestQuestionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.danciyixue.boot.dcyx.model.form.TestQuestionsForm;
import com.danciyixue.boot.dcyx.model.query.TestQuestionsQuery;
import com.danciyixue.boot.dcyx.model.vo.TestQuestionsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 测试题目前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:30
 */
@Tag(name = "测试题目接口")
@RestController
@RequestMapping("/api/v1/test-questions")
@RequiredArgsConstructor
public class TestQuestionsController  {

    private final TestQuestionsService testQuestionsService;

    @Operation(summary = "测试题目分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('dcyx:test-questions:query')")
    public PageResult<TestQuestionsVO> getTestQuestionsPage(TestQuestionsQuery queryParams ) {
        IPage<TestQuestionsVO> result = testQuestionsService.getTestQuestionsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增测试题目")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('dcyx:test-questions:add')")
    public Result<Void> saveTestQuestions(@RequestBody @Valid TestQuestionsForm formData ) {
        boolean result = testQuestionsService.saveTestQuestions(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取测试题目表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('dcyx:test-questions:edit')")
    public Result<TestQuestionsForm> getTestQuestionsForm(
        @Parameter(description = "测试题目ID") @PathVariable Long id
    ) {
        TestQuestionsForm formData = testQuestionsService.getTestQuestionsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改测试题目")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('dcyx:test-questions:edit')")
    public Result<Void> updateTestQuestions(
            @Parameter(description = "测试题目ID") @PathVariable Long id,
            @RequestBody @Validated TestQuestionsForm formData
    ) {
        boolean result = testQuestionsService.updateTestQuestions(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除测试题目")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('dcyx:test-questions:delete')")
    public Result<Void> deleteTestQuestionss(
        @Parameter(description = "测试题目ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = testQuestionsService.deleteTestQuestionss(ids);
        return Result.judge(result);
    }
}
